﻿namespace MicroCloud.Api.Authorization.Areas.Admin.Controllers.Auth
{
    /// <summary>
    /// 实体管理
    /// </summary>
    [Description("实体管理")]
    [ModuleInfo(Code = "EntityInfo", Name = "实体管理", OrderNo = 1, Position = "Auth", PositionName = "授权模块")]
    public class EntityInfoController : AdminControllerBase
    {
        /// <summary>
        /// 获取 数据权限管理器
        /// </summary>
        protected DataAuthManager DataAuthManager => ServiceProvider.GetService<DataAuthManager>();

        #region "Action方法"
        /// <summary>
        /// 读取实体
        /// </summary>
        /// <param name="request">分页请求</param>
        /// <returns>实体信息集合</returns>
        [HttpPost]
        [ModuleInfo]
        [Description("读取")]
        public virtual PageData<EntityInfoOutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<EntityInfo, bool>> predicate = FilterService.GetExpression<EntityInfo>(request.FilterGroup);
            request.AddDefaultSortCondition(new SortCondition("TypeName", ListSortDirection.Ascending));

            var page = CacheService.ToPageCache<EntityInfo, EntityInfoOutputDto>(DataAuthManager.EntityInfos, predicate, request.PageCondition, function);
            return page.ToPageData();
        }

        /// <summary>
        /// 读取实体节点
        /// </summary>
        /// <returns>实体节点集合</returns>
        [HttpGet]
        [ModuleInfo]
        [Description("读取节点")]
        public virtual List<EntityInfoNode> ReadNode()
        {
            IFunction function = this.GetExecuteFunction();
            Expression<Func<EntityInfo, bool>> exp = m => true;

            List<EntityInfoNode> nodes = CacheService.ToCacheList<EntityInfo, EntityInfoNode>(DataAuthManager.EntityInfos, exp, function);
            return nodes;
        }

        /// <summary>
        /// 读取实体属性
        /// </summary>
        /// <param name="typeName">实体类型</param>
        /// <returns>JSON操作结果</returns>
        [HttpGet]
        [ModuleInfo]
        [Description("读取属性")]
        public virtual AjaxResult ReadProperties(string typeName)
        {
            Check.NotNull(typeName, nameof(typeName));

            IFunction function = this.GetExecuteFunction();
            Expression<Func<EntityInfo, bool>> exp = m => m.TypeName == typeName;

            string json = CacheService.ToCacheList(DataAuthManager.EntityInfos, exp, m => m.PropertyJson, function).FirstOrDefault();
            //string json = _dataAuthManager.EntityInfos.FirstOrDefault(m => m.TypeName == typeName)?.PropertyJson;
            if (json == null)
            {
                return AjaxResult.Error(I18N.T("实体类 {0} 不存在", typeName));
            }
            string[] filterTokens = { "Normalized", "Stamp", "Password" };
            EntityProperty[] properties = json.FromJsonString<EntityProperty[]>().Where(m => !filterTokens.Any(n => m.Name.Contains(n))).OrderByDescending(m => m.Name == "Id").ToArray();

            return AjaxResult.Success(properties);
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="dtos">实体信息</param>
        /// <returns>JSON操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [DependOnFunction(nameof(ReadNode))]
        [DependOnFunction(nameof(ReadProperties))]
        [Description("更新")]
        public virtual async Task<AjaxResult> Update(params EntityInfoInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            OperationResult result = await DataAuthManager.UpdateEntityInfos(dtos);
            return result.ToAjaxResult(true);
        }

        #endregion

    }
}